package com.ganmiao.server.supplier.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ganmiao.server.supplier.entity.SupplierUser;
import com.ganmiao.server.supplier.repository.SupplierUserRepository;
import com.ganmiao.utils.StatusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SupplierUserService {

    @Autowired
    private SupplierUserRepository supplierUserRepository;

    /**
     * 添加供应商关联用户
     *
     * @param supplierUser
     * @return
     */
    public boolean add(SupplierUser supplierUser) {
        return supplierUserRepository.save(supplierUser);
    }

    /**
     * 删除供应商关联用户
     *
     * @param userId
     * @return
     */
    public boolean delete(Integer userId) {
        LambdaUpdateWrapper<SupplierUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SupplierUser::getUserId, userId)
                .set(SupplierUser::getStatus, StatusUtils.FAILURE);
        return supplierUserRepository.update(updateWrapper);
    }

    /**
     * 获取供应商主键ID (用户ID查询)
     *
     * @param userId
     * @return
     */
    public SupplierUser getSupplierId(Integer userId) {
        LambdaQueryWrapper<SupplierUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SupplierUser::getUserId, userId);
        return supplierUserRepository.getOne(queryWrapper);
    }

}
